def font_file_to_css(file: dict) -> str:
    url = file['file']

    fmt = file['format']

    return f'url({url}) format("{fmt}")'


def unicode_range_to_css(urange: dict) -> str:
    start = urange['start']

    end = urange['end']

    return f'U+{start}-{end}'


def font_to_css(
    font: dict,
    name: str | None = None,
    style: str = 'normal',
    weight: int | str = '400'
) -> str:
    files = font['files']

    uranges = dict()

    for file in files:
        css = ', '.join(map(unicode_range_to_css, file['unicode_ranges']))

        if not css:
            css = 'unset'

        if css not in uranges:
            uranges[css] = []

        uranges[css].append(file)

    result = ''

    for urange in uranges:
        files = uranges[urange]

        urls = ','.join(map(font_file_to_css, files))

        fname = name or font['name']

        rules = {
            'font-family': f'"{fname}"',
            'src': urls,
            'font-display': 'swap',
            'font-style': style,
            'font-weight': weight
        }

        if urange != 'unset':
            rules['unicode-range'] = urange

        body = ''

        for key, value in rules.items():
            key = key.replace('_', '-')

            body += f'\n\t{key}: {value};'

        result += '@font-face {' + body + '\n}\n\n'

    return result[:-2]  # This removes the last \n\n not needed for formatting
